<?php

/**
 * contactUs actions.
 *
 * @package    sf_sandbox
 * @subpackage contactUs
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class contactUsActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeTellAFriend() {
        // todo TellAFriend
    }
    public function executeIndex()
    {
        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_CONTACT);
        $this->customerService = AppSettingPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_ADDRESS);
        $this->customerAddress = AppSettingPeer::doSelectOne($c);
    }

    public function executeInfo() {
        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_CONTACT);
        $this->customerService = AppSettingPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_ADDRESS);
        $this->customerAddress = AppSettingPeer::doSelectOne($c);
    }

    public function executeSendMail()
    {
        $cms_contact_us = new CmsContactUs();
        $cms_contact_us->setFullName($this->getRequestParameter('contactname'));
        $cms_contact_us->setEmail($this->getRequestParameter('email'));
        $cms_contact_us->setMessage($this->getRequestParameter('enquiry'));
        $cms_contact_us->setStatusCode(Globals::STATUS_ACTIVE);

        if ($this->getUser()->getAttribute(Globals::SESSION_USERID)) {
            $cms_contact_us->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));
            $cms_contact_us->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));
        } else {
            $cms_contact_us->setCreatedBy(Globals::SYSTEM_USER_ID);
            $cms_contact_us->setUpdatedBy(Globals::SYSTEM_USER_ID);
        }
        $cms_contact_us->save();

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::CUSTOMER_SERVICE_EMAIL);
        $customerEmail = AppSettingPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::EMAIL_HOST);
        $emailHost = AppSettingPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::EMAIL_FROM_NOREPLY);
        $emailFromNoreply = AppSettingPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(AppSettingPeer::PARAMETER, Globals::EMAIL_FROM_NOREPLY_NAME);
        $emailFromNoreplyName = AppSettingPeer::doSelectOne($c);

        /****************************/
        /*****  Send email **********/
        /****************************/
        error_reporting(E_STRICT);

        date_default_timezone_set(date_default_timezone_get());
        include_once('class.phpmailer.php');

        $subject = $this->getContext()->getI18N()->__("Walao.com.my contact us", null, 'email');
        $body = "Name : ". $this->getRequestParameter('contactname');
        $body .= "<p>Email : ". $this->getRequestParameter('email');
        $body .= "<p>Message : ". $this->getRequestParameter('enquiry');

        $mail = new PHPMailer();
        $mail->IsMail(); // telling the class to use SMTP
        $mail->Host = $emailHost->getValue(); // SMTP server
        $mail->Sender = $emailFromNoreply->getValue();
        $mail->From = $emailFromNoreply->getValue();
        $mail->FromName = $emailFromNoreplyName->getValue();

        $mail->Subject = $subject;
        $text_body = $body;

        $mail->Body = $body;
        $mail->AltBody = $text_body;
        $mail->AddAddress($customerEmail->getValue(), "customer service");
        $mail->AddBCC("r9jason@gmail.com", "jason");

        if (!$mail->Send())
            echo $mail->ErrorInfo;

        return $this->redirect('contactUs/info?msg=Successful');
    }
}
